package com.zhq.apputil.widget;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhq.apputil.R;
import com.zhq.apputil.utils.StringUtil;
import com.zhq.apputil.widget.listener.OnRetryListener;

import androidx.annotation.LayoutRes;


/**
 * 封装了4种状态的View：正在加载、加载失败、没有数据、正常界面
 *
 * @author dzl
 */
public class StateLayout extends FrameLayout {

    private View loadingView;
    private View failView;
    private View emptyView;
    private View contentView;
    private TextView tvContent;
    /**
     * 包含了3种状态(正在加载、加载失败、没有数据) 的一个容器
     */
    private FrameLayout container;
    private int btnResource;
    private OnRetryListener onRetryListener;
    private int resource;
    private String tvAlertContent;
    private int imageResource;

    public StateLayout(Builder builder) {
        super(builder.context);
        this.resource = builder.resource;
        this.btnResource = builder.btnId;
        this.imageResource = builder.imageResource;
        this.tvAlertContent = builder.tvAlertContent;
        this.onRetryListener = builder.onRetryListener;
        initView();
    }

    public void initView() {
        // 创建出来的容器已经包含了3种状态：正在加载、加载失败、没有数据
        container = (FrameLayout) View.inflate(getContext(), resource, null);
        // 查找出3种状态对应的View
        loadingView = container.findViewById(R.id.AVLoadingIndicatorView);
        failView = container.findViewById(R.id.failView);
        emptyView = container.findViewById(R.id.emptyView);
        tvContent = container.findViewById(R.id.tv_empty_message);
        if (btnResource != 0) {
            container.findViewById(btnResource).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRetryListener != null) {
                        onRetryListener.onRetry();
                    }
                }
            });
        }
        if (!StringUtil.isEmpty(tvAlertContent)) {
            tvContent.setText(tvAlertContent);
        }
        if (imageResource != 0) {
            ImageView imageView = container.findViewById(R.id.imageBackground);
            imageView.setImageResource(imageResource);
        }
        showLoadingView();
        addView(container);
    }


    /**
     * 显示“正在加载。。。”
     */
    public void showLoadingView() {
        showView(loadingView);
    }

    /**
     * 显示“加载失败”
     */
    public void showFailView() {
        showView(failView);
    }

    /**
     * 显示“加载为空”
     */
    public void showEmptyView() {
        showView(emptyView);
    }

    public void showAlertView(String content) {
         showView(emptyView);
        if (!StringUtil.isEmpty(content)) {
            tvContent.setText(content);
        }
    }


    /**
     * 显示“正常加载到数据”的界面的View
     */
    public void showContentView() {
        showView(contentView);
    }


    /**
     * 设置“正常状态的View”
     */
    public void setContentView(int layoutResId) {
        View contentView = View.inflate(getContext(), layoutResId, null);
        this.setContentView(contentView);
    }

    /**
     * 设置“正常状态的View”
     */
    public void setContentView(View contentView) {
        this.contentView = contentView;
        container.addView(contentView);
        contentView.setVisibility(View.GONE);   // 默认不显示，默认显示的是LoadingView

    }

    /**
     * 指示指定的View，并隐藏其它的View
     */
    private void showView(View view) {
        // System.out.println("childCount = " + container.getChildCount());
        for (int i = 0; i < container.getChildCount(); i++) {
            View child = container.getChildAt(i);
            child.setVisibility(child == view ? View.VISIBLE : View.GONE);
        }
    }


    public static final class Builder {
        private Context context;
        private int resource;
        private int btnId;
        private int imageResource;
        private String tvAlertContent;
        private OnRetryListener onRetryListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setStateLayout(@LayoutRes int resource) {
            this.resource = resource;
            return this;
        }

        public Builder setStateLayoutViewId(int btnId) {
            this.btnId = btnId;
            return this;
        }

        public Builder setStateLayoutAlertContent(String tvAlertContent) {
            this.tvAlertContent = tvAlertContent;
            return this;
        }

        public Builder setStateLayoutImageResrouce(int imageResource) {
            this.imageResource = imageResource;
            return this;
        }

        public Builder onRetryListener(OnRetryListener onRetryListener) {
            this.onRetryListener = onRetryListener;
            return this;
        }

        public StateLayout build() {
            return new StateLayout(this);
        }


    }

    public void unBindListener() {
        onRetryListener = null;
    }
}
